const Router = require('koa-router');
const router = new Router({ prefix: '/users' });
const { findeId, create, find, update: modify, del, login, checkOwner, listFollowing, checkUserExist, follow, unfollow, listFollower,followTopic, unfollowTopic, checkTopicExist,listFollowingTopics, listQuestions, listLikingAnswers, likeAnswer, unlikeAnswer, listDisLikingAnswers, dislikeAnswer, undislikeAnswer } = require("../controllers/users");
const { checkAnswercExist } = require('../controllers/Answer');
//自己编写的中间件
// const jsonwebtoken = require('jsonwebtoken');
const jwt = require('koa-jwt');
const { secret } = require('../config');

//认证token的中间件(自己编写的)
// const auth = async(ctx,next)=>{
//     const { authorization = '' } = ctx.request.header;
//     const token = authorization.replace('Bearer ','');
//     try {
//         const user = jsonwebtoken.verify(token, secret);
//         ctx.state.user = user;
//     } catch (error) {
//         ctx.throw(401, error.message)
//     }
//     await next();
// }

//使用插件
const auth = jwt({ secret })
router.get('/:id', findeId)
router.get('/', find)
router.delete('/del/:id', auth, checkOwner, del)
router.post('/post', create)
router.patch('/modify/:id', auth, checkOwner, modify)
//登录
router.post('/login', login)
//关注取消粉丝
//获取某个用户关注人的列表
router.get('/:id/following', listFollowing);
//获取粉丝列表
router.get('/:id/follower', listFollower);
//关注某人
router.put('/following/:id', auth, checkUserExist, follow);
//取消关注某人
router.delete('/unfollowing/:id', auth, checkUserExist, unfollow);
//获取某个用户关注话题的列表
router.get('/:id/listFollowingTopics', listFollowingTopics);
//关注话题
router.put('/followingTopics/:id', auth, checkTopicExist, followTopic);
//取消关注话题
router.delete('/unfollowingTopics/:id', auth, checkTopicExist, unfollowTopic);
//查询用户的问题列表
router.get('/:id/questions', listQuestions);

//获取喜欢答案的列表
router.get('/:id/likingAnswers', listLikingAnswers);
//喜欢答案
router.put('/likingAnswers/:id', auth, checkAnswercExist, likeAnswer,undislikeAnswer);
//取消喜欢答案
router.delete('/likingAnswers/:id', auth, checkAnswercExist, unlikeAnswer);
//获取踩答案的列表
router.get('/:id/dislikingAnswers', listDisLikingAnswers);
//踩答案
router.put('/dislikingAnswers/:id', auth, checkAnswercExist, dislikeAnswer,unlikeAnswer);
//取消踩答案
router.delete('/dislikingAnswers/:id', auth, checkAnswercExist, undislikeAnswer);

module.exports = router;